package cn.zy.org.controller;

import cn.zy.basic.annotation.PreAuthorize;
import cn.zy.basic.util.AjaxResult;
import cn.zy.basic.util.PageList;
import cn.zy.org.domain.Employee;
import cn.zy.org.query.EmployeeQuery;
import cn.zy.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 接口类:部门
 */
@Api(value = "部门的API",description="部门相关的CRUD功能") //打在Controller对Controller做描述
@RestController //前端都要发送异步请求,需要加上@ResponseBody注解,所有使用复合注解@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService IEmployeeService;


    /**
     * 接口:查询单个
     */
    @ApiOperation(value = "接口:通过ID查询" ) //打在方法上，对方法做描述
    @GetMapping("/{id}")
    @PreAuthorize(name = "查询单个员工",sn = "employee:getOne")
    public Employee findOne(@PathVariable("id") Long id){
        return IEmployeeService.loadById(id);
    }

    /**
     * 接口:查询所有
     * @return
     */
    @ApiOperation(value = "接口:查询所有" )
    @GetMapping
    @PreAuthorize(name = "查询所有员工",sn = "employee:getAll")
    public List<Employee> findAll(){
        return IEmployeeService.loadAll();
    }

    /**
     * 接口:删除
     * @param id
     * @return
     */
    @ApiOperation(value = "接口:删除" )
    @DeleteMapping("/{id}")
    @PreAuthorize(name = "删除单个员工",sn = "employee:delete")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            IEmployeeService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败!");
        }
    }

    /**
     * 接口:添加和修改
     * @param employee
     * @return
     */
    @ApiOperation(value = "接口:添加和修改" )
    @PutMapping
    @PreAuthorize(name = "保存员工",sn = "employee:save")
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
        try {
            if(employee.getId()==null){//添加
                IEmployeeService.save(employee);
            }else {
                IEmployeeService.update(employee);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败!");
        }
    }

    /**
     * 接口:分页和高级查询
     * @param employeeQuery
     * @return
     */
    @ApiOperation(value = "接口:分页和高级查询" )
    @PostMapping
    @PreAuthorize(name = "员工列表",sn = "employee:list")
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery employeeQuery){
        return IEmployeeService.queryPage(employeeQuery);
    }

    /**
     * 接口:批量删除
     * @param ids
     * @return
     */
    @ApiOperation(value = "接口:批量删除" )
    @PatchMapping
    @PreAuthorize(name = "员工批量删除",sn = "employee:patchDel")
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            IEmployeeService.patchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败!");
        }
    }

    /**
     * 查询指定店铺的所有人员
     * @param shopId
     * @return
     */
    @GetMapping("/list/{shopId}")
    public List<Employee> queryByShopId(@PathVariable("shopId")Long shopId) {
        return IEmployeeService.queryByShopId(shopId);
    }
}
